﻿using System;
using GC_FFW.BusinessEntities.Abstractions.Interfaces.Forecast;
using GC_FFW.ForecastWriter.Enums;
using GC_FFW.ForecastWriterClient.Abstractions.Factories;
using GC_FFW.ForecastWriterClient.Abstractions.Interfaces;
using GC_FFW.ForecastWriterClient.Abstractions.Interfaces.Managers;
using GC_FFW.ForecastWriterClient.Abstractions.Interfaces.ViewModels.Parameters;
using GC_FFW.ForecastWriterClient.Enums;
using GC_FFW.ForecastWriterClient.ViewModels.Parameters;
using GC_FFW.SoccerfameParser.Enums;

namespace GC_FFW.ForecastWriterClient.Managers
{
	public class WindowManager : IWindowManager
	{
		#region Variables

		private readonly WindowAdapterBaseFactory _windowAdapterFactory;

		#endregion

		#region Constructors

		public WindowManager(WindowAdapterBaseFactory windowAdapterFactory)
		{
			if (windowAdapterFactory == null)
				throw new ArgumentNullException(string.Format("windowAdapterFactory"));
			_windowAdapterFactory = windowAdapterFactory;
		}

		#endregion

		#region IWindowManager Members

		public WindowDialogStatus ShowForecastManagerWindow()
		{
			var parameters = new ForecastManagerViewModelParameters();
			FillViewModelParameters(parameters);
			var adapter = _windowAdapterFactory.CreateForecastManagerWindowAdapter(parameters);
			return ShowWindow(adapter);
		}

		public WindowDialogStatus ShowMatchDataEditorWindow(string url, MatchType type)
		{
			var parameters = new MatchDataEditorViewModelParameters
			{
				Url = url,
				MatchType = type
			};
			FillViewModelParameters(parameters);
			var adapter = _windowAdapterFactory.CreateMatchDataEditorWindowAdapter(parameters);
			return ShowWindow(adapter);
		}

		public WindowDialogStatus ShowMatchEventsEditorWindow(IMatch match, Action<IMatch> updateAction)
		{
			var parameters = new MatchEventsEditorViewModelParameters
			{
				Match = match,
				UpdateAction = updateAction
			};
			var adapter = _windowAdapterFactory.CreateMatchEventsEditorWindowAdapter(parameters);
			return ShowWindow(adapter);
		}

		public WindowDialogStatus ShowForecastEditorWindow(IForecastData forecastData, MatchType type)
		{
			var parameters = new ForecastEditorViewModelParameters
			{
				MatchType = type,
				ForecastData = forecastData
			};
			var adapter = _windowAdapterFactory.CreateForecastEditorWindowAdapter(parameters);
			return ShowWindow(adapter);
		}

		public WindowDialogStatus ShowImportDataManagerWindow(ImportDataType type)
		{
			var parameters = new ImportDataManagerViewModelParameters
			{
				ImportDataType = type
			};
			var adapter = _windowAdapterFactory.CreateImportDataManagerWindowAdapter(parameters);
			return ShowWindow(adapter);
		}

		#endregion

		#region Internal Implementation

		private WindowDialogStatus ShowWindow(IWindow adapter)
		{
			if (adapter == null) return WindowDialogStatus.Cancel;
			adapter.ShowDialog();
			return adapter.WindowDialogStatus;
		}

		private void FillViewModelParameters(IPrimaryViewModelParameters parameters)
		{
			if (parameters == null) return;
			parameters.WindowManager = this;
		}

		#endregion
	}
}
